<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-07-25 16:54:15
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Pro;


use App\Http\Services\Service;
use App\Repositories\AttachmentRepository;
use App\Repositories\ProColorMyRepository;
use App\Repositories\ProColorRepository;
use App\Repositories\ProColorRevItemRepository;
use App\Repositories\ProColorRevRepository;
use App\Repositories\ProColorRevItemHistoryRepository;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

/**
 * @property ProColorRepository $repository
 * @property ProColorRevRepository $rev
 * @property ProColorRevItemRepository $rev_list
 * @property ProColorRevItemHistoryRepository $rev_history
 * @property AttachmentRepository $attachment
 */
class ColorService extends Service
    {
    public function boot(
        ProColorRepository $repository,
        ProColorRevRepository $rev,
        ProColorRevItemRepository $rev_list,
        ProColorRevItemHistoryRepository $rev_history,
        AttachmentRepository $attachment
    ) {
        $this->repository  = $repository;
        $this->rev         = $rev;
        $this->rev_list    = $rev_list;
        $this->rev_history = $rev_history;
        $this->attachment  = $attachment;
        }

    public function list()
        {
        // DB::enableQueryLog();
        $data = $this->repository->condition(request())
            ->where("code", "<>", DB::raw("concat('X',id)"))
            ->whereCustonly(0)
            ->getGrid();
        // dd(DB::getQueryLog());

        return $data;
        }
    public function list_all()
        {
        $data = $this->repository->condition(request())
            ->whereCustonly(0)
            ->getGrid();
        return $data;
        }
    public function details()
        {
        $data = $this->rev->condition(request())->getGrid();
        return $data;
        }


    protected function save()
        {
        $data            = request()->toArray();
        $revision        = $data['revision'];
        $details         = $data['details'];
        $main            = $revision['main'];
        $main['details'] = $details;

        // $attachment = $this->attachment->whereId($main['attachments']['0']['id'])->first();
        // if ($attachment) {
        //     $rgb = readImageRgb(Storage::path($attachment->filethumbnail));
        //     unset($main['rgb']);
        //     if ($rgb !== false) {
        //         $main['rgb'] = $rgb;
        //         }
        //     }  
        $this->repository->autosave($main);
        $revision['color_material_code'] = $revision['main_id'] . "_" . $revision['material_id'];
        $revision['company_id']          = $main['company_id'];
        return $this->rev->autosave($revision, true, ['items']);
        }
    protected function delete($request)
        {
        return $this->repository->delete($this->getRequestIds());
        }
    protected function deleteRevision($request)
        {
        return $this->rev->with(['items'])->whereIn("id", $this->getRequestIds())->autodelete();
        }

    protected function faudit()
        {
        $request = request();

        $ids = [];

        $rev = $this->rev->whereId($request->id)->getOne();

        if ($rev->status_id == 2)
            fail("已终审,色号:[{$rev->main->code}]当前状态:[{$rev->status->name}],不可以终审！");
        $rev->status_id = 2;
        $rev->save();

        foreach ($request->list as $data) {
            $rev_list       = $this->rev_list->whereId($data['color_item_id'])->first();
            $rev_list->rate = $data['newrate'];
            $ids[]          = $this->rev_history->autosave($this->rev_history->initializeFieldsData($data))->id;

            $rev_list->save();
            }

        return $ids;
        }
    public function fauditlist()
        {
        return $this->rev_history->whereMain_id(request("id"))->getList();
        }
    protected function auditGood()
        {
        $olddata = $this->rev->whereId(request("id"))->first();

        $data = $this->rev->autosave(request()->toArray());
        //产品修改了，原色卡颜色删除，重新新增
        if ($olddata['material_id'] != $data['material_id']) {
            ProColorMyRepository::run("addMyColor", [$data, false]);
            }
        return ProColorMyRepository::run("addMyColor", [$data]);
        }
    protected function audit()
        {
        return $this->rev->with(['items'])->whereIn("id", $this->getRequestIds())->getList()->map(function ($item)
            {
            if ($item->status_id != 0) {
                fail("未初审状态才可以初审,色号:[{$item->main->code}]当前状态:[{$item->status->name}],不可以初审！");
                }
            $item->status_id = 1;
            $item->save();
            return $item->id;
            });
        }
    protected function nofaudit()
        {
        return $this->rev->with(['items'])->whereIn("id", $this->getRequestIds())->getList()->map(function ($item)
            {
            if ($item->status_id != 2) {
                fail("终审状态才可以反终审,色号:[{$item->main->code}]当前状态:[{$item->status->name}],不可以反终审！");
                }
            $item->status_id = 1;
            $item->save();
            return $item->id;
            });
        }
    protected function noaudit()
        {
        return $this->rev->with(['items'])->whereIn("id", $this->getRequestIds())->getList()->map(function ($item)
            {
            if ($item->status_id != 1) {
                fail("初审状态才可以反初审,色号:[{$item->main->code}]当前状态:[{$item->status->name}],不可以反初审！");
                }
            $item->status_id = 0;
            $item->save();
            return $item->id;
            });
        }
    protected function noauditGood()
        {
        return $this->rev->with(['items'])->whereIn("id", $this->getRequestIds())->getList()->map(function ($item)
            {
            return ProColorMyRepository::run("addMyColor", [$item, false]);
            });
        }
    }